<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/12
 * Time: 15:08
 */
namespace App\Http\Controllers\Admin;

use GuzzleHttp\Client;
use App\Service\Admin\CloudService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class CloudController extends BaseController
{
    protected $service;

    public function __construct(Request $request, CloudService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 创建直播间
     * @return \Illuminate\Http\JsonResponse
     */
    public function cloudCreate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'live_title'    =>  'required|string',
            'start_time'    =>  'required|date',
            'end_time'      =>  'required|date',
            'live_desc'     =>  'required|string',
            'host_id'       =>  'required|integer|exists:mini_anchor,id',
            'main_img'      =>  'required|string'
        ], [
            'live_title.required'  =>  '直播标题不能为空',
            'start_time.required'  =>  '计划开始时间不能为空',
            'end_time.required'    =>  '计划结束时间不能为空',
            'live_desc.required'   =>  '直播描述不能为空',
            'host_id.required'     =>  '主播不能为空',
            'main_img.required'    =>  '直播主图不能为空',

            'live_title.string'    =>  '直播标题必须为字符',
            'live_desc.string'     =>  '直播描述必须为字符',
            'main_img.string'      =>  '直播主图必须为字符',

            'start_time.date'      =>  '计划开始时间必须为时间格式',
            'end_time.date'        =>  '计划结束时间必须为时间格式',

            'host_id.integer'      =>  '主播ID必须为整型',
            'host_id.exists'       =>  '主播不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_LIVE_CREATE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->createCloud($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_LIVE_CREATE_ERROR, $result);
        }
    }
}
